SHELL=/bin/sh

include config.mk

BUILD=build
PROG=src/ax
MAN_IT=lang/it_IT/ax.1
MAN=lang/en_EN/ax.1
ETC=etc/client
L_IT=lang/it_IT/ax

.PHONY: all clean dist install uninstall


all:
	$(CECHO)sed -i 's/^PREFIX=.*$$/PREFIX=\"$(subst /,\/,$(PREFIX))\"/g' $(PROG)
	$(CECHO)sed -i 's/^ETCPREFIX=.*$$/ETCPREFIX=\"$(subst /,\/,$(ETCPREFIX))\"/g' $(PROG)
	$(CECHO)sed -i "s/^\.I .*\/ax\/client$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/client/g" $(MAN)
	$(CECHO)sed -i "s/^\.I .*\/ax\/script.sh\$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/script.sh/g" $(MAN)
	$(CECHO)sed -i "s/^\.I .*\/ax\/args.sh\$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/args.sh/g" $(MAN)
	$(CECHO)sed -i "s/^\.I .*\/ax\/post.sh\$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/post.sh/g" $(MAN)
	$(CECHO)sed -i "s/^\.I .*\/ax\/lang\$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/lang/g" $(MAN)
	$(CECHO)sed -i "s/^\.I .*\/ax\/client$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/client/g" $(MAN_IT)
	$(CECHO)sed -i "s/^\.I .*\/ax\/script.sh\$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/script.sh/g" $(MAN_IT)
	$(CECHO)sed -i "s/^\.I .*\/ax\/args.sh\$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/args.sh/g" $(MAN_IT)
	$(CECHO)sed -i "s/^\.I .*\/ax\/post.sh\$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/post.sh/g" $(MAN_IT)
	$(CECHO)sed -i "s/^\.I .*\/ax\/lang\$$/.I $(subst /,\/,$(ETCPREFIX))\/etc\/ax\/lang/g" $(MAN_IT)

clean:
	@printf "Cleaning project\n"
	$(CECHO)rm -rf $(BUILD)/*

dist: clean
	@printf "creating dist tarball\n"
	$(CECHO)mkdir -p ax-$(VERSION)/src/
	$(CECHO)mkdir -p ax-$(VERSION)/lang/it_IT/
	$(CECHO)mkdir -p ax-$(VERSION)/lang/en_EN/
	$(CECHO)mkdir -p ax-$(VERSION)/etc/
	$(CECHO)mkdir -p ax-$(VERSION)/$(BUILD)/
	$(CECHO)cp -R configure Makefile config.mk LICENSE README.md README_ita.md \
		USAGE.txt ax-$(VERSION)
	$(CECHO)cp -R $(PROG) ax-$(VERSION)/src/
	$(CECHO)cp -R $(L_IT) ax-$(VERSION)/lang/it_IT/
	$(CECHO)cp -R $(MAN) ax-$(VERSION)/lang/en_EN/
	$(CECHO)cp -R $(MAN_IT) ax-$(VERSION)/lang/it_IT/
	$(CECHO)cp -R $(ETC) ax-$(VERSION)/etc/
	$(CECHO)tar -cf ax-$(VERSION).tar ax-$(VERSION)
	$(CECHO)gzip ax-$(VERSION).tar
	$(CECHO)rm -rf ax-$(VERSION)
	$(CECHO)mv ax-$(VERSION).tar.gz $(BUILD)

install: all
	@printf "installing shell file to $(PREFIX)/bin\n"
	$(CECHO)mkdir -p $(PREFIX)/bin
	$(CECHO)cp -f $(PROG) \
		$(PREFIX)/bin/$(subst src/,,$(PROG))
	$(CECHO)chmod 755 $(PREFIX)/bin/$(subst src/,,$(PROG))

	@printf "installing italian locale file to $(ETCPREFIX)/etc/ax\n"
	$(CECHO)mkdir -p $(ETCPREFIX)/etc/ax/lang/it_IT
	$(CECHO)cp -f $(L_IT) \
		$(ETCPREFIX)/etc/ax/$(L_IT)
	$(CECHO)chmod 755 $(ETCPREFIX)/etc/ax/$(L_IT)

	@printf "installing man file to $(MANPREFIX)/share/man/man1\n"
	$(CECHO)mkdir -p $(MANPREFIX)/share/man/man1
	$(CECHO)cp -f $(MAN) \
		$(MANPREFIX)/share/man/man1/$(subst lang/en_EN/,,$(MAN))
	$(CECHO)chmod 644 $(MANPREFIX)/share/man/man1/$(subst lang/en_EN/,,$(MAN))

	@printf "installing italian man file to $(MANPREFIX)/share/man/it/man1\n"
	$(CECHO)mkdir -p $(MANPREFIX)/share/man/it/man1
	$(CECHO)cp -f $(MAN_IT) \
		$(MANPREFIX)/share/man/it/man1/$(subst lang/it_IT/,,$(MAN_IT))
	$(CECHO)chmod 644 $(MANPREFIX)/share/man/it/man1/$(subst lang/it_IT/,,$(MAN_IT))

	@printf "installing config file to $(ETCPREFIX)/etc/ax\n"
	$(CECHO)mkdir -p $(ETCPREFIX)/etc/ax
	$(CECHO)cp -f $(ETC) \
		$(ETCPREFIX)/etc/ax/$(subst etc/,,$(ETC))
	$(CECHO)chmod 644 $(ETCPREFIX)/etc/ax/$(subst etc/,,$(ETC))

uninstall:
	@printf "removing shell file from $(PREFIX)/bin\n"
	$(CECHO)rm -f $(PREFIX)/bin/$(subst src/,,$(PROG))

	@printf "removing italian locale file from $(ETCPREFIX)/etc/ax\n"
	$(CECHO)rm -f $(ETCPREFIX)/etc/ax/$(L_IT)

	@printf "removing man file from $(MANPREFIX)/share/man/man1\n"
	$(CECHO)rm -f $(MANPREFIX)/share/man/man1/$(subst lang/en_EN/,,$(MAN))

	@printf "removing italian man file from $(MANPREFIX)/share/man/it/man1\n"
	$(CECHO)rm -f $(MANPREFIX)/share/man/it/man1/$(subst lang/it_IT/,,$(MAN_IT))

	@printf "removing config file from $(ETCPREFIX)/etc/ax\n"
	$(CECHO)rm -f $(ETCPREFIX)/etc/ax/$(subst etc/,,$(ETC))
